<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
    (function (window, undefined) {

        function aop(type) {

            return function (target, methodName, advice) {

                var exist = target[methodName], dispatcher;
                if (!exist || exist.target != target) {
                    console.log(1)
                    dispatcher = target[methodName] = function () {
                        // before methods
                        var beforeArr = dispatcher.before;
                        var args = arguments;
                        for (var l = beforeArr.length; l--;) {
                            args = beforeArr[l].advice.apply(this, args) || args;
                        }
                        // target method
                        var rs = dispatcher.method.apply(this, args);
                        // after methods
                        var afterArr = dispatcher.after;
                        for (var i = 0, ii = afterArr.length; i < ii; i++) {
                            rs = afterArr[i].advice.call(this, rs, args) || rs;
                        }
                        // return object
                        return rs;
                    }

                    dispatcher.before = [];
                    dispatcher.after = [];

                    if (exist) {
                        dispatcher.method = exist;
                    }
                    dispatcher.target = target;
                }

                var aspectArr = (dispatcher || exist)[type];
                var obj = {
                    advice: advice,
                    _index: aspectArr.length,
                    remove: function () {
                        aspectArr.splice(this._index, 1);
                    }
                };
                aspectArr.push(obj);

                return obj;
            };
        }

        window.aop = {
            before: aop("before"),
            after: aop("after")
        };

        return window.aop;

    })(window);

    var obj = {
        url: "",
        get: function (key) {
            return this[key];
        },
        set: function (key, value) {
            this[key] = value;
            console.log(value)
        }
    };

    var h1 = aop.before(obj, "set", function (key, value) {
        value += " before-1 ";
        console.log(value);
        return [key, value];
    });

    var h2 = aop.after(obj, "set", function (key, value) {
        value += " after-1 ";
        console.log(value);
    });

    obj.set("url", "http://mojijs.com");
    obj.get("url");

</script>
</body>
</html>